package DCSource.ConfigManager;

import DCSource.InfoTool;
import DCSource.dcsrc;
import DCSource.ConfigManager.Class;
import DCSource.ConfigManager.Item;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Manager{
	private int rand(int low, int high) {  
        high++;  
        return (int) (Math.random() * (high - low) + low);  
    }
	private boolean isWindowsSystem()
	  { String osName = System.getProperty("os.name").toLowerCase();
	    return osName.indexOf("windows") >= 0;
	  }
	  
	private boolean isLinuxSystem()
	  { String osName = System.getProperty("os.name").toLowerCase();
	    return osName.indexOf("linux") >= 0;
	  }
	private String umsch(){
		if (isWindowsSystem()==true){
			return String.valueOf((char)13)+String.valueOf((char)10);
		}else{return String.valueOf((char)10);}
	}
	private ArrayList<Class> list = new ArrayList();
	private String name=rand(0,999999999)+""+rand(0,999999999);
	private String version=InfoTool.getVersion();
	public Manager(){}
	public Manager(Class cls){
		list.add(cls);
	}
	public Manager(String title){
		name=title;
	}
	public Manager(String title,Class item){
		name=title;
		list.add(item);
	}
	public Manager(Class item,String title){
		name=title;
		list.add(item);
	}
	public Manager(Class[] mgs){
		for (int I=0;I<mgs.length;I++){
			list.add(mgs[I]);
		}
	}
	private String compileToItem(String Text){
		Text = Text.replace(String.valueOf((char) 13), "{&001;}");
		Text = Text.replace("=", "{&002;}");
		Text = Text.replace(";;", "{&003;}");
		Text = Text.replace(";-;","{&004;}");
		Text = Text.replace(";/;","{&005;}");
		Text = Text.replace("main","{&006;}");
		Text = Text.replace(String.valueOf((char) 10), "{&007;}");
		Text = Text.replace("{&001;}{&007;}","{&008;}");
		Text = Text.replace(";:;","{&009;}");
		Text = Text.replace("name","{&010;}");
		Text = Text.replace(";.;","{&011;}");
		Text = Text.replace("version","{&012;}");
		Text = Text.replace(";MG;", "{&013;}");
		Text = Text.replace("[", "{&014;}");
		Text = Text.replace("]", "{&015;}");
		return Text;
	}
	private String decompile(String Text){
		Text = Text.replace("{&008;}","{&001;}{&007;}");
		Text = Text.replace("{&001;}",String.valueOf((char) 13));
		Text = Text.replace("{&002;}","=");
		Text = Text.replace("{&003;}",";;");
		Text = Text.replace("{&004;}",";-;");
		Text = Text.replace("{&005;}",";/;");
		Text = Text.replace("{&006;}","main");
		Text = Text.replace("{&007;}",String.valueOf((char) 10));
		Text = Text.replace("{&009;}",";:;");
		Text = Text.replace("{&010;}","name");
		Text = Text.replace("{&011;}",";.;");
		Text = Text.replace("{&012;}","version");
		Text = Text.replace("{&013;}", ";MG;");
		Text = Text.replace("{&014;}","[");
		Text = Text.replace("{&015;}", "]");
		return Text;
	}
	private boolean searchIn(String Text,String Search){
		boolean erg = false;
		for (int I=0;I<Math.round(Text.length()/Search.length());I+=Search.length()){
			String Value = Text.substring(I, I+Search.length());
			if (Value.equals(Search)){
				erg=true;
			}
		}
		return erg;
	}
	public void setTitle(String title){
		name = title;
	}
	public String getTitle(){
		if (name!=null){
			return (name);}else{
			return ("");
			}
	}
	public int getCount(){
		return list.size();
	}
	public Class getClass(String title){
		Class returning = new Class("");
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(title)){
				returning=list.get(I);
			}
		}
		return returning;
	}
	public Class getClass(int dex){
		Class returning = new Class("");
		if (dex<list.size()&dex>-1){
			returning=list.get(dex);
		}
		return returning;
	}
	public Class[] getall(){
		return list.toArray(new Class[list.size()]);
	}
	public Item getItem(Class cl,String title){
		Item returning = new Item("");
		for (int I=0;I<cl.getCount();I++){
			if (cl.getItem(I).getTitle().equalsIgnoreCase(title)){
				returning=cl.getItem(I);
			}
		}
		return returning;
	}
	public Item getItem(String cl,String title){
		Item returning = new Item("");
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				for (int In=0;In<list.get(I).getCount();In++){
					if (list.get(I).getItem(In).getTitle().equals(title)){
						returning = list.get(I).getItem(In);
					}
				}
			}
		}
		return returning;
	}
	public Item getItem(String cl,int dex){
		Item returning = new Item("");
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(cl)){
				if (dex<list.get(I).getCount() & dex>-1){
					returning=list.get(I).getItem(dex);
				}
			}
		}
		return returning;
	}
	public String getValue(Class cl,String title){
		String returning = "";
		for (int I=0;I<cl.getCount();I++){
			if (cl.getItem(I).getTitle().equals(title)){
				returning=cl.getItem(I).getValue();
			}
		}
		return returning;
	}
	public String getValue(String cl,String title){
		String returning = "";
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equals(cl)){
				for (int In=0;In<list.get(I).getCount();In++){
					if (list.get(I).getItem(In).getTitle().equals(title)){
						returning = list.get(I).getItem(In).getValue();
					}
				}
			}
		}
		return returning;
	}
	public String getValue(Class cl,Item it){
		String returning = "";
		for (int I=0;I<cl.getCount();I++){
			if (cl.getItem(I).getTitle().equalsIgnoreCase(it.getTitle())){
				returning=cl.getItem(I).getValue();
			}
		}
		return returning;
	}
	public String getValue(String cl,Item it){
		String returning = "";
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				for (int In=0;In<list.get(I).getCount();In++){
					if (list.get(I).getItem(In).getTitle().equalsIgnoreCase(it.getTitle())){
						returning = list.get(I).getItem(In).getValue();
					}
				}
			}
		}
		return returning;
	}
	public String getValue(String cl,int dex){
		String returning = "";
		if (dex>-1){
			for (int I=0;I<list.size();I++){
				if (list.get(I).getTitle().equalsIgnoreCase(cl)){
					if (dex<list.get(I).getCount()){
						returning = list.get(I).getItem(dex).getValue();
					}
				}
			}
		}
		return returning;
	}
	public String getValue(Class cl,int dex){
		String returning = "";
		if (dex>-1){
			for (int I=0;I<list.size();I++){
				if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
					if (dex<list.get(I).getCount()){
						returning = list.get(I).getItem(dex).getValue();
					}
				}
			}
		}
		return returning;
	}
	public String getTitle(Class cl){
		return cl.getTitle();
	}
	public String getTitle(String cl){
		String returning = "";
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				returning=list.get(I).getTitle();
			}
		}
		return returning;
	}
	public String getTitle(int cl){
		String returning="";
		if (cl>-1&cl<list.size()){
			returning = list.get(cl).getTitle();
		}
		return returning;
	}
	public String getTitle(Class cl,String title){
		String returning = "";
		for (int I=0;I<cl.getCount();I++){
			if (cl.getItem(I).getTitle().equalsIgnoreCase(title)){
				returning=cl.getItem(I).getTitle();
			}
		}
		return returning;
	}
	public String getTitle(String cl,String title){
		String returning = "";
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				for (int In=0;In<list.get(I).getCount();In++){
					if (list.get(I).getItem(In).getTitle().equalsIgnoreCase(title)){
						returning = list.get(I).getItem(In).getTitle();
					}
				}
			}
		}
		return returning;
	}
	public String getTitle(String cl,int dex){
		String returning = "";
		if (dex>-1){
			for (int I=0;I<list.size();I++){
				if (list.get(I).getTitle().equalsIgnoreCase(cl)){
					if (dex<list.get(I).getCount()){
						returning = list.get(I).getItem(dex).getTitle();
					}
				}
			}
		}
		return returning;
	}
	public String getTitle(Class cl,int dex){
		String returning = "";
		if (dex>-1){
			for (int I=0;I<list.size();I++){
				if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
					if (dex<list.get(I).getCount()){
						returning = list.get(I).getItem(dex).getTitle();
					}
				}
			}
		}
		return returning;
	}
	public void addItem(Class cl,Item st,boolean overwrite){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
				list.get(I).addItem(st, overwrite);
			}
		}
	}
	public void addItem(String cl,Item st,boolean overwrite){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				list.get(I).addItem(st, overwrite);
			}
		}
	}
	public void addItem(Class cl,String title,String value ,boolean overwrite){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
				list.get(I).addItem(new Item(title,value), overwrite);
			}
		}
	}
	public void addItem(String cl,String title,String value ,boolean overwrite){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				list.get(I).addItem(new Item(title,value), overwrite);
			}
		}
	}
	public void addClass(Class cl,boolean overwrite){
		boolean found=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
				if (overwrite==true){
					list.set(I,cl);
					found=true;
				}else{found=true;}
			}
		}
		if (found==false){
			list.add(cl);
		}
	}
	public void addClass(String title,boolean overwrite){
		boolean found=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(title)){
				if (overwrite==true){
					list.set(I,new Class(title));
					found=true;
				}else{found=true;}
			}
		}
		if (found==false){
			list.add(new Class(title));
		}
	}
	public void compare(Manager toAdd, boolean overwrite){
		for (Class c : toAdd.getall()){
			if (this.existClass(c)){
				this.getClass(c.getTitle()).compare(c,overwrite);
			}else{
				this.addClass(c, overwrite);
			}
		}
	}
	public void compareClass(String c, Class toAdd, boolean overwrite){
		this.getClass(c).compare(toAdd, overwrite);
	}
	public void compareClass(int c, Class toAdd, boolean overwrite){
		this.list.get(c).compare(toAdd, overwrite);
	}
	public void compareClass(Class c, Class toAdd, boolean overwrite){
		this.getClass(c.getTitle()).compare(toAdd, overwrite);
	}
	public void compareClass(String c, Item[] toAdd, boolean overwrite){
		this.getClass(c).compare(toAdd, overwrite);
	}
	public void compareClass(int c, Item[] toAdd, boolean overwrite){
		this.list.get(c).compare(toAdd, overwrite);
	}
	public void compareClass(Class c, Item[] toAdd, boolean overwrite){
		this.getClass(c.getTitle()).compare(toAdd, overwrite);
	}
	public void compare(Class[] toAdd, boolean overwrite){
		for (Class c : toAdd){
			if (this.existClass(c)){
				this.getClass(c.getTitle()).compare(c,overwrite);
			}else{
				this.addClass(c, overwrite);
			}
		}
	}
	public void revClass(Class cl){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
				list.remove(I);
				I--;
			}
		}
	}
	public void revClass(String title){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(title)){
				list.remove(I);
				I--;
			}
		}
	}
	public void revClass(int dex){
		if (dex>-1&dex<list.size()){
			list.remove(dex);
		}
	}
	public void revItem(Class cl,Item it){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
				for (int In=0;In<list.get(I).getCount();In++){
					if (list.get(I).getItem(In).getTitle().equalsIgnoreCase(it.getTitle())){
						list.get(I).revItem(In);
						In--;
					}
				}
			}
		}
	}
	public void revItem(Class cl,String title){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
				for (int In=0;In<list.get(I).getCount();In++){
					if (list.get(I).getItem(In).getTitle().equalsIgnoreCase(title)){
						list.get(I).revItem(In);
						In--;
					}
				}
			}
		}
	}
	public void revItem(String cl,Item it){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				for (int In=0;In<list.get(I).getCount();In++){
					if (list.get(I).getItem(In).getTitle().equalsIgnoreCase(it.getTitle())){
						list.get(I).revItem(In);
						In--;
					}
				}
			}
		}
	}
	public void revItem(String cl,String it){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				for (int In=0;In<list.get(I).getCount();In++){
					if (list.get(I).getItem(In).getTitle().equalsIgnoreCase(it)){
						list.get(I).revItem(In);
						In--;
					}
				}
			}
		}
	}
	public void revItem(String cl,int dex){
		if (dex>-1){
			for (int I=0;I<list.size();I++){
				if (list.get(I).getTitle().equalsIgnoreCase(cl)){
					if (dex<list.get(I).getCount()){
						list.get(I).revItem(dex);
					}
				}
			}
		}
	}
	public void revItem(Class cl,int dex){
		if (dex>-1){
			for (int I=0;I<list.size();I++){
				if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
					if (dex<list.get(I).getCount()){
						list.get(I).revItem(dex);
					}
				}
			}
		}
	}
	public void revAllClasses(){
		list.clear();
	}
	public void revAllItems(Class cl){
		cl.revAllItems();
	}
	public void revAllItems(String cl){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				list.get(I).revAllItems();
			}
		}
	}
	public Class[] getClasses(){
		ArrayList<Class> liste = new ArrayList();
		for (int I=0;I<list.size();I++){
			liste.add(list.get(I));
		}
		return (Class[]) liste.toArray();
	}
	public boolean existClass(Class cl){
		boolean returning=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
				returning=true;
			}
		}
		return returning;
	}
	public boolean existClass(String cl){
		boolean returning=false;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				returning=true;
			}
		}
		return returning;
	}
	public boolean saveToFile(String uri){
		String source = "";
		if (name!=null){
		source += "name="+name;}
		source += umsch()+"version="+version;
		for (int I=0;I<list.size();I++){
			source += umsch()+"["+compileToItem(list.get(I).getTitle())+"]";
			for (int In=0;In<list.get(I).getCount();In++){
				source+=umsch()+compileToItem(list.get(I).getItem(In).getTitle())+"="+compileToItem(list.get(I).getItem(In).getValue());
			}
		}
		try{
			FileOutputStream write = new FileOutputStream(uri);
			write.write(source.getBytes());
			write.close();
			return true;
		}catch(Exception err){return false;}
	}
	public String getSource(){
		String source = "";
		if (name!=null){
		source += "name="+name;}
		source += umsch()+"version="+version;
		for (int I=0;I<list.size();I++){
			source += umsch()+"["+compileToItem(list.get(I).getTitle())+"]";
			for (int In=0;In<list.get(I).getCount();In++){
				source+=umsch()+compileToItem(list.get(I).getItem(In).getTitle())+"="+compileToItem(list.get(I).getItem(In).getValue());
			}
		}
		return source;
	}
	public void loadIn(String url){
		list.clear();
		String name=rand(0,999999999)+""+rand(0,999999999);
		version=InfoTool.getVersion();
		String returning="";
		try
	    {
	      String link = url;
	      File file=new File(link);
	      FileInputStream inputstream=new FileInputStream(file);
	      InputStreamReader reader=new InputStreamReader(inputstream);
	      BufferedReader br=new BufferedReader(reader);
	      String line;
	      while ((line = br.readLine()) != null) {
	         returning+=line+String.valueOf((char) 10);
	      }
	    } catch (Exception exp) {}
		String[] lines=returning.split(String.valueOf((char) 10));
		String selClass = "";
		if (lines.length>0){
			for (int I=0;I<lines.length;I++){
				boolean use=false;
				if (lines[I].startsWith("version")==true&searchIn(lines[I],"=")==true){
					use=true;
					String[] Version = lines[I].split("=");
					this.version=Version[1];
				}
				if (lines[I].startsWith("name")==true&searchIn(lines[I],"=")==true){
					use=true;
					String[] Name = lines[I].split("=");
					this.name=Name[1];
				}
				if (use==false&searchIn(lines[I],"=")){
					if (lines[I].split("=").length>1){
						use=true;
						//this.addItem(selClass, new Item(decompile(lines[I].split("=")[0]),decompile(lines[I].split("=")[1])), true);
						this.addItem(selClass,new Class("loader").getItemByDoingstr(lines[I]) , true);
					}
				}
				if (use==false&lines[I].startsWith("[")&lines[I].endsWith("]")){
					selClass = decompile(lines[I].replace("[","").replace("]",""));
					this.addClass(decompile(lines[I].replace("[","").replace("]","")),true);
				}
			}
		}
	}
	public boolean LoadIn(String file){
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			list.clear();
			String name=rand(0,999999999)+""+rand(0,999999999);
			version=InfoTool.getVersion();
			String line = "";
			String selClass = "";
			while((line=in.readLine())!=null){
				boolean use=false;
				if (line.startsWith("version")==true&searchIn(line,"=")==true){
					use=true;
					String[] Version = line.split("=");
					this.version=Version[1];
				}
				if (line.startsWith("name")==true&searchIn(line,"=")==true){
					use=true;
					String[] Name = line.split("=");
					this.name=Name[1];
				}
				if (use==false&searchIn(line,"=")){
					use=true;
					this.addItem(selClass,new Class("loader").getItemByDoingstr(line) , true);
				}
				if (use==false&line.startsWith("[")&line.endsWith("]")){
					selClass = decompile(line.replace("[","").replace("]",""));
					this.addClass(decompile(line.replace("[","").replace("]","")),true);
				}
			}
		} catch (Exception e) {
			return false;
		}
		return true;
		
	}
	public void setSource(String source){
		list.clear();
		String name=rand(0,999999999)+""+rand(0,999999999);
		version=InfoTool.getVersion();
		String returning=source;
		returning = returning.replace(String.valueOf((char) 13), "");
		String[] lines=returning.split(String.valueOf((char) 10));
		String selClass = "";
		if (lines.length>0){
			for (int I=0;I<lines.length;I++){
				boolean use=false;
				if (lines[I].startsWith("version")==true&searchIn(lines[I],"=")==true){
					use=true;
					String[] Version = lines[I].split("=");
					this.version=Version[1];
				}
				if (lines[I].startsWith("name")==true&searchIn(lines[I],"=")==true){
					use=true;
					String[] Name = lines[I].split("=");
					this.name=Name[1];
				}
				if (use==false&searchIn(lines[I],"=")){
					if (lines[I].split("=").length>1){
						use=true;
						//this.addItem(selClass, new Item(decompile(lines[I].split("=")[0]),decompile(lines[I].split("=")[1])), true);
						this.addItem(selClass,new Class("loader").getItemByDoingstr(lines[I]) , true);
					}
				}
				if (use==false&lines[I].startsWith("[")&lines[I].endsWith("]")){
					selClass = decompile(lines[I].replace("[","").replace("]",""));
					this.addClass(decompile(lines[I].replace("[","").replace("]","")),true);
				}
			}
		}
	}
	public String GetAll(String format){
		String returning = "Unknow format! Use a clear format to see it in the default format!";
		if (format.equalsIgnoreCase("default")){format="";}
		if (format.equalsIgnoreCase("DEF")){format="";}
		if (format.equalsIgnoreCase("html")){format="htm";}
		if (format.equalsIgnoreCase("")){
			returning="Name: "+name+umsch()+"Version: "+version+umsch();
			for (int I=0;I<list.size();I++){
				returning += list.get(I).getTitle()+umsch();
				for (int In=0;In<list.get(I).getCount();In++){
					returning += " -"+list.get(I).getItem(In).getTitle()+" - \""+list.get(I).getItem(In).getValue()+"\""+umsch();
				}
			}
		}
		if (format.equalsIgnoreCase("htm")){
			returning="Name: "+name+"<br />"+"Version: "+version+"<br />";
			for (int I=0;I<list.size();I++){
				returning += list.get(I).getTitle()+":<br /><table rules='all'>";
				for (int In=0;In<list.get(I).getCount();In++){
					returning += "<tr><td> -"+list.get(I).getItem(In).getTitle()+"</td><td> - \""+list.get(I).getItem(In).getValue().replace(umsch(), "<br />")+"\""+"</td></tr>";
				}
				returning += "</table>";
			}
		}
		return returning;
	}
	public String[] getFormats(){
		return new String[]{"default","def","html","htm"};
	}
	public void clearClass(String cl){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl)){
				list.get(I).Clear();
			}
		}
	}
	public void clearClass(Class cl){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTitle().equalsIgnoreCase(cl.getTitle())){
				list.get(I).Clear();
			}
		}
	}
	public String toString(){
		String text = name+">>>";
		for (int I=0;I<list.size();I++){
			text += list.get(I).toString()+";";
		}
		return "{"+text+"}";
	}
}
